﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Hr
{
    public partial class frmManagePosition : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstPositionEntity mSelectedPosition;
        #endregion

        #region Constructor
        public frmManagePosition()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtPositionCode.Text) == false)
            {
                condition += " AND POSITION_CODE LIKE '%" + txtPositionCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtPositionName.Text) == false)
            {
                condition += " AND POSITION_NAME LIKE '%" + txtPositionName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbDeleteFlag.Text) == false && cmbDeleteFlag.SelectedValue != null)
            {
                condition += " AND DELETE_FLAG = " + cmbDeleteFlag.SelectedValue.ToString() + "";
            }
            if (string.IsNullOrEmpty(cmbPositionLevel.Text) == false && cmbPositionLevel.SelectedValue != null)
            {
                condition += " AND LEVEL = '" + cmbPositionLevel.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "POSITION_CODE";
            DataTable dt = MstPositionBs.GetPositionByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        private void frmManagePosition_Load(object sender, EventArgs e)
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeleteFlag, typeof(DeleteFlag), typeof(string), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colDeleteFlag, typeof(DeleteFlag), typeof(int), true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPositionLevel, "PLEVEL", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colLevel, "PLEVEL", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPosition = new MstPositionEntity(dr);
            if (mSelectedPosition.DeleteFlag == 1)
            {
                btnExport.Enabled = false;
            }
            else
            {
                btnExport.Enabled = true;
            }
        }

        protected override void Click_View()
        {
            if (mSelectedPosition == null)
            {
                MessageBox.Show("请选择一个职位！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPosition frm = new frmMaintainPosition();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPosition = mSelectedPosition;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedPosition == null)
            {
                MessageBox.Show("请选择一个职位！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中职位？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedPosition.DeleteFlag = 1;
                bool successful = MstPositionBs.UpdateMstPosition(mSelectedPosition);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPosition == null)
            {
                MessageBox.Show("请选择一个职位！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPosition frm = new frmMaintainPosition();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedPosition = mSelectedPosition;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainPosition frm = new frmMaintainPosition();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Export()
        {
            if (MstPositionBs.GenerateRoles(CommonParameter.CurrentUser.PersonId))
            {
                BindDataGrid();
                MessageBox.Show("生成成功！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        #endregion


    }
}
